package com.hzjh.h5mall.model.web

import com.hzjh.base.ext.frame.JHBaseVM
import com.hzjh.h5mall.bean.UpFileResBean
import com.hzjh.h5mall.http.MallApi
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

class JSBridgeModel:JHBaseVM() {
    //图片上传
    fun uploadImage(url: String,result:(UpFileResBean?)->Unit){
        makeCall<UpFileResBean> {
            onRequest {
                val file = File(url)
                val requestFile =
                    file.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                val fileBody =
                    MultipartBody.Part.createFormData("multipart", file.name, requestFile)
                MallApi.create().uploadCommImage(fileBody)
            }

            onSuccess {
                result(it)
            }

            onError { errMsg.value = it }
        }
    }
}